import { TOKEN } from '@/store/const'
import { storageLocal } from '@wxcn/utils'
import { createRouter, createWebHistory } from 'vue-router'
import { routes } from './routes'

const router = createRouter({
  history: createWebHistory(),
  routes,
  scrollBehavior(params) {
    // 始终滚动到顶部
    if (!params.hash) {
      return { top: 0 }
    }
  },
})
router.beforeEach(async (to, _, next) => {
  const token = storageLocal.getItem(TOKEN)
  if (to.meta.auth && !token) {
    next(`/sign/in?redirect=${to.path}`)
  }
  else {
    next()
  }
})
export default router
